package com.back.domain.utils;

import com.back.domain.VO.SYSTEM.SysDeptTreeNode;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class TreeBuilder {
    public static SysDeptTreeNode buildTreeUtil(List<String[]> data) {
        Map<Integer, SysDeptTreeNode> nodeMap = new HashMap<>();
        SysDeptTreeNode root = null;

        for (String[] row : data) {
            int id = Integer.parseInt(row[0]);
            String name = row[1];
            int parentId = Integer.parseInt(row[2]);

            SysDeptTreeNode node = new SysDeptTreeNode(id, name);
            nodeMap.put(id, node);

            if (parentId == 0) {
                root = node;
            } else {
                SysDeptTreeNode parent = nodeMap.get(parentId);
                if (parent != null) {
                    parent.getChildren().add(node);
                }
            }
        }
        return root;
    }
}
